package iii.myangel;


import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import packet.AskLocation;
import packet.selectLocation;



import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;

import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public  class MapRecordActivity extends Activity implements LocationListener {
	public static MapRecordActivity gmap;
	public static Handler handler= new Handler();
	private LatLng l_latKH;
	private GoogleMap map;
	private LocationManager locationManager;
	private Criteria criteria;
	private Location myLocation;
	private String provider;
	private Double Latitude,longitude;
	private long locationtime;
	private String returnAddress;
	int id;
	
	//接收後更新地圖
	private void getPacket(final double Latitude, final double longitude, final long locationtime) { 
//		lbllat.setText("由XXXX收到訊息: " + Latitude + ", " + longitude + ", " + locationtime);
		this.Latitude= Latitude; 
		this.longitude= longitude;
		this.locationtime=locationtime;
		

		//呼叫google map並送座標進去
		l_latKH = new LatLng(Latitude, longitude);
		
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		Marker l_markKHStation = map.addMarker(new MarkerOptions()
		.position(l_latKH).title("我是老人1").snippet("我在這"));
		
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(l_latKH, 16));
		
		//定位時間
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy/MM/dd/ HH:mm:ss");
		Date dt = new Date(locationtime);
		String sDateTime = sdf.format(dt);
		//定位地址
		Geocoder gc = new Geocoder(this, Locale.TRADITIONAL_CHINESE);
		List<Address> lstAddress;
		try {
			lstAddress = gc.getFromLocation(Latitude, longitude, 1);
			returnAddress=lstAddress.get(0).getThoroughfare()+lstAddress.get(0).getFeatureName()+"號";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		Toast.makeText(this, "我在這", Toast.LENGTH_SHORT).show();

		
		lbltime.setText("時間:"+sDateTime+"");
		lbladdress.setText("地址:"+returnAddress+"");
	}
	
	//接收socket送回來的封包
	public static  Runnable OXOX(final double Latitude, final double longitude, final long locationtime) {
		return new Runnable() {
			@Override
			public void run() {
				gmap.getPacket(Latitude, longitude, locationtime);
			}
		};
	}

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_maprecord);
		InitialComponent();
		
		SharedPreferences sp=getSharedPreferences("olduser", 0);
		id=sp.getInt("id", 0);
		lblname.setText(sp.getString("name", "")+id);
		
		askOldmanLocation();

		
		gmap=this;
		
		
		
	}
	private void askOldmanLocation(){
		//送老人要求取GPS送SQL
		AskLocation a=new AskLocation();
		a.id=id;
		MainActivity.lcl.send(a);
		
		//送SQL取的GPS
		selectLocation p=new selectLocation();
		p.id=id;
		MainActivity.lcl.send(p);		
		
	}

	@SuppressLint("NewApi")
	public void map(double Latitude,double longitude,long locationtime){
	
	}
	// 在TextView上更新自己位置的相關資訊
	private void updateMyLocationInfo(Location myLocation) {
		
		StringBuilder msg = new StringBuilder();
		msg.append("自己位置相關資訊 \n");

		// 取得定位時間
		Date date = new Date(myLocation.getTime());
		DateFormat dateFormat = DateFormat.getDateTimeInstance();
		String time = dateFormat.format(date);
		msg.append("定位時間: " + time + "\n");

		// 取得自己位置的緯經度、精準度、高度、方向與速度，不提供的資訊會回傳0.0
		msg.append("緯度: " + myLocation.getLatitude() + "\n");
		msg.append("經度: " + myLocation.getLongitude() + "\n");
		msg.append("精準度(公尺): " + myLocation.getAccuracy() + "\n");
		msg.append("高度(公尺): " + myLocation.getAltitude() + "\n");
		msg.append("方向(角度): " + myLocation.getBearing() + "\n");
		msg.append("速度(公尺/秒): " + myLocation.getSpeed() + "\n");

		
	}
	
	
	@Override
	public void onLocationChanged(Location arg0) {

		
	}
	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
	OnClickListener btnBack_click = new OnClickListener() {
		public void onClick(View v) {
			
									
			Intent intent = new Intent(MapRecordActivity.this,
					MainControlActivity.class);
			startActivity(intent);
			
		}
	};
	
	
	
	private void InitialComponent() {
	
		lbltime=(TextView)findViewById(R.id.lbltime);
		lbladdress=(TextView)findViewById(R.id.lbladdress);
		lblname=(TextView)findViewById(R.id.lblname);
		btnBack = (ImageButton) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(btnBack_click);

	}
	
	TextView lbltime=null;
	TextView lbladdress=null;
	TextView lblname=null;
	ImageButton btnBack=null;
}
